<?php

namespace addons\facrm\model;


class Notices extends \app\admin\model\facrm\Notices
{

    /**
     * 添加通知
     * @param $content
     * @param $engine
     * @param int $admin_id
     * @param int $customer_id
     * @param int $status
     * @param int $user_id
     */
    public static function add($content="", $engine="", $status = 0, $msg = "", $extend = [])
    {
        $insert_data = [
            'content' => is_array($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
            'engine' => $engine,
            'status' => $status,
            'msg' => $msg,
            'customer_id' => 0,
            'admin_id' => 0,
            'user_id' => 0,
            'create_time' => time(),
        ];

        $insert_data = array_merge($insert_data, $extend);
        return self::create($insert_data);

    }

    /**
     * 编辑或添加
     * @param $event_id
     * @param $extend
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function edit($event_id, $extend)
    {
        $row = self::where('event_id', $event_id)->find();

        if ($row) {

            return $row->save($extend);
        } else {
            $extend['event_id']=$event_id;
            $extend['content']=isset($extend["content"])&& is_array($extend["content"]) ? json_encode($extend["content"] ,JSON_UNESCAPED_UNICODE) : "";
            return self::add("", "", 0,"",$extend);
        }

    }
}
